package org.csu.code.api.controller;

import net.sf.json.JSONObject;
import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.Product;
import org.csu.code.api.entity.Sentence;
import org.csu.code.api.service.TalkService;
import org.csu.code.api.service.impl.TalkServiceImpl;
import org.csu.code.api.tools.talkPart.WebSocketServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/talk")
public class TalkController {
    @Autowired
    private TalkService talkService = new TalkServiceImpl();

//    @GetMapping("/list/{type}/{username}")
//    @ResponseBody
//    public CommonResponse<?>  get(@PathVariable("type")String type, @PathVariable("username")String username){
//        if(type.equals("name"))
//        {
//            return talkService.getNameList();
////            JSONObject object = new JSONObject();
////            object.put("status","OK");
////            object.put("msg","获取对话列表成功!");
////            object.put("data",talkService.getNameList());
////            return object.toString();
//        }
//        else if(type.equals("talk"))
//        {
//            return talkService.getTalkList(username);
////            JSONObject object = new JSONObject();
////            object.put("status","OK");
////            object.put("msg","获取对话列表成功!");
////            object.put("data",talkService.getTalkList(username));
////            return object.toString();
//        }
//        else{
////            JSONObject object = new JSONObject();
////            object.put("status","NOK");
////            object.put("msg","传入参数类型错误!");
////            object.put("data",null);
//            return CommonResponse.createForError("传入参数类型错误!");
//        }
//    }

    @PostMapping("/insert")
    @ResponseBody
    public CommonResponse<String> insert(@RequestBody Sentence sentence,HttpServletRequest request){
        try {
            WebSocketServlet.sendInfo(sentence);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        String s =
                return talkService.insertSentence(request,sentence);
//        if(s.equals("发送成功!")){
//            JSONObject object = new JSONObject();
//            object.put("status","OK");
//            object.put("msg","发送成功!");
//            object.put("data",null);
//            return object.toString();
//        }
//        else
//        {
//            JSONObject object = new JSONObject();
//            object.put("status","NOK");
//            object.put("msg","发送失败!");
//            object.put("data",null);
//            return object.toString();
//        }
    }
    @GetMapping("/talklist/{touserid}")
    @ResponseBody
    public CommonResponse<String>  getTotalTalk(@PathVariable("touserid")int touserid,HttpServletRequest request){
        return talkService.getTotalTalk(request,touserid);
    }
}
